discard

set more off  

 use "$healthsave\disputes solved_w7.dta", clear 

/** now we should have a consistent series of 'ever diagnosed' variables for 
	everyone. Check this  **/ 

foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
	forv i = 1/7 { 
	forv j = 1/7 { 
		if `i' < `j' { 
		di in red "`x' `i' `j'" 
			assert ever`x'_w`i' <= ever`x'_w`j'	| ever`x'_w`i' ==. | ever`x'_w`j'==-1 
	}
	}
	}
	}
	

*/

	
/***************************/
/** code up date of onset **/
/***************************/


lab define howcl 10 "From date of diagnosis reported later" , modify 


	** we don't have information on onset dates for every condition; 
	** only: 	an mi hf di st ar ca pd ps ad de bl 

	** there are three variables that give timing of onset. 
	** 		aged`x' 
	**		yrd`x' 
	** 		mnthd`x' 
	
	** for angina AND heart attacks, there is no information 
	** on the timing of diagnoses. 
	
	foreach x in an mi hf di st ar ca pd ps ad de bl { 
		su aged`x'_w? if ffproblem`x' == 1 
		su yrd`x'* if ffproblem`x' ==1  
	}
		
	** oop! a few valid observations for year 
	** diagnosed with angina/heart attack in waves 5/6/7 and 6 respectively. 
	** these seem to be fine - restricted to new reports. 
		
	** make sure noone reports a year of diagnosis AFTER 
	
	** 	group 1: reported year of onset is consistent with onset during 
	** 			 ELSA survey 
	
	**  group 2: reported onset is EARLIER than onset during ELSA survey 
	
	
	**  group 3: reported onset is AFTER onset during ELSA survey 
	** 			 most likely to happen during dispute rejections 
	** 			(keep this information for stroke/cancer/heart attack onsets) 
	
	
	
	
	** construct indicator for implied wave of onset ** 
		
	foreach x in an mi hf di st ar ca pd ps ad de bl { 
		forv i = 1/7 { 
		format yrd`x'_w`i' %ty 
		*format mnthd`x'_w`i' %tm 
		gen onsetdate`x'`i' = ym(yrd`x'_w`i' , mnthd`x'_w`i') if yrd`x'_w`i' >0 & mnthd`x'_w`i'>0  
		format onsetdate`x'`i' %tm 
		}
	}
		
		** sometimes month is missing. 
		** if mnth is missing and reported year of diagnosis is equal to 
		** a previous interview wave, set month equal to month 
		** that intvw + 1 
		
		** if not, set month equal to jan 

	
	foreach x in an mi hf di st ar ca pd ps ad de bl { 
		forv i = 2/7 { 
		local Pi = `i'-1 
			replace onsetdate`x'`i' = ym(yrd`x'_w`i' , 1) ///
				if (yrd`x'_w`i' != intyear_w1 & yrd`x'_w`i' != intyear_w2 & yrd`x'_w`i' != intyear_w3 & yrd`x'_w`i' != intyear_w4 & yrd`x'_w`i' != intyear_w5 & yrd`x'_w`i' != intyear_w6) ///
				 & yrd`x'_w`i' >0 & yrd`x'_w`i' !=. ///	/* yr of diagnosis non-missing */
				 & mnthd`x'_w`i' <=0 		/* month of diagnosis missing */
		
			forv w = 1/7 { 
			replace onsetdate`x'`i' = ym(yrd`x'_w`i' , intmonth_w`w' +1 ) ///
				if intmonth_w`w'!=12 ///
				& (yrd`x'_w`i' == intyear_w`w') ///
				 & yrd`x'_w`i' >0 & yrd`x'_w`i' !=. ///	/* yr of diagnosis non-missing */
				 & mnthd`x'_w`i' <=0 		/* month of diagnosis missing */
			
			replace onsetdate`x'`i' = ym(yrd`x'_w`i' +1 , 1 ) ///
				if intmonth_w`w'==12 ///
				& (yrd`x'_w`i' == intyear_w`w') ///
				 & yrd`x'_w`i' >0 & yrd`x'_w`i' !=. ///	/* yr of diagnosis non-missing */
				 & mnthd`x'_w`i' <=0 		/* month of diagnosis missing */
					
			}		
		}
	}

	
	** use reported AGE of onset to fill in onset dates 
	
	** first work out which wave is the LAST wave that respondent 
	** is <= reported age of diagnosis. 
	
	** onset date is == that wave if CONTEMPORANEOUS report 
	** onset date is == that wave + 1 month if LATER report 
	
	
	foreach x in an mi hf di st ar ca pd ps ad de bl { 
		forv w = 1/7 { 		/* wave of report */
		
			gen lastwave_notd`x'_w`w' = . 	
		
		forv i = 1/7 { 		/* wave to be referenced */
			if `i' < `w' {
			replace lastwave_notd`x'_w`w' = `i' if agew`i' <= aged`x'_w`w'  & agew`i'!=. & aged`x'_w`w'!=. 
			}
			
			if `i' == `w' { 
			replace lastwave_notd`x'_w`w' = `i'-1 if agew`i' == aged`x'_w`w' & agew`i'!=. 
			}	
		}		
		replace lastwave_notd`x'_w`w' = firstw if lastwave_notd`x'_w`w' ==. & aged`x'_w`w' >0 & aged`x'_w`w'!=. 
		}	
	}

		
	foreach x in an mi hf di st ar ca pd ps ad de bl { 
	forv i = 1/7 { 
	forv w = 1/7 { 
		replace onsetdate`x'`i' = ym(intyear_w`w' , intmonth_w`w' +1) ///
				if lastwave_notd`x'_w`i' == `w' &  intmonth_w`w'!=12 
		
		replace onsetdate`x'`i' = ym(intyear_w`w' + 1 , 1) ///
				if lastwave_notd`x'_w`i' == `w' &  intmonth_w`w'==12 
	} 
	}
	}
	
	
	
foreach x in an mi hf di st ar ca pd ps ad de bl { 	
	forv i = 2/7 { 
	local Pi = `i' -1 
		di in red "`x' wave `i'"
		count if onsetdate`x'`i' < intdate_w`Pi' 
		count if onsetdate`x'`i' < intdate_w`Pi' & onsetdate`x'`i' >= intdate_w`Pi'-2 
		count if onsetdate`x'`i' >= intdate_w`Pi' & onsetdate`x'`i' !=.
		
		/** adding in margin for error **/	
		gen onsetdatecleaned`x'`i' = onsetdate`x'`i' < intdate_w`Pi' & onsetdate`x'`i' >= intdate_w`Pi'-2 & inwave`Pi'==1 
		replace onsetdate`x'`i' = intdate_w`Pi' + 1 if onsetdate`x'`i' < intdate_w`Pi' & onsetdate`x'`i' >= intdate_w`Pi'-2 & inwave`Pi'==1 
		lab var onsetdatecleaned`x'`i' "Onset date shifted up to 2 months later because likely recall error" 
	}
}
	
** make sure we are not using onset dates for accepted disputes. 

foreach x in an mi hf di st ar ca pd ps ad de bl { 	
	forv i = 1/7 { 
		replace onsetdate`x'`i' = . ///
				if (howcl`x'_w`i' == 5 | howcl`x'_w`i' == 6) & ever`x'_w`i' == 0 
	}
}

* BE CAREFUL re onset dates for rejected disputes. 

** => recoding dates should only ADD waves where onsets have happened. 
** 		should therefore be ok... 


** sometimes people report two onset dates in different waves. 
** where the second is EARLIER than the first, 
** trust the first. 

	foreach x in an mi hf di st ar ca pd ps ad de bl { 	
		forv i = 1/7 { 	
		forv j = 1/7 { 
			if `i' < `j' { 
			replace onsetdate`x'`i' = . if onsetdate`x'`j' < onsetdate`x'`i' 
		}
		} 
	}
	}

		
	foreach x in an mi hf di st ar ca pd ps ad de bl { 	
		forv i = 1/7 { 	
		replace howcl`x'_w`i' = 10 if alive_w`i' == 1 ///
			& 	ever`x'_w`i' !=1  ///
			&  ((onsetdate`x'1 < intdate_w`i') ///
			| 	(onsetdate`x'2 < intdate_w`i') ///
			|   (onsetdate`x'3 < intdate_w`i') ///
			|   (onsetdate`x'4 < intdate_w`i') ///
			|   (onsetdate`x'5 < intdate_w`i') ///
			|   (onsetdate`x'6 < intdate_w`i') ///
			|   (onsetdate`x'7 < intdate_w`i'))
			
		replace ever`x'_w`i' = 1 if alive_w`i' == 1 ///
			&  ((onsetdate`x'1 < intdate_w`i') ///
			| 	(onsetdate`x'2 < intdate_w`i') ///
			|   (onsetdate`x'3 < intdate_w`i') ///
			|   (onsetdate`x'4 < intdate_w`i') ///
			|   (onsetdate`x'5 < intdate_w`i') ///
			|   (onsetdate`x'6 < intdate_w`i') ///
			|   (onsetdate`x'7 < intdate_w`i')) 
		}		
	}
		
	foreach x in an mi hf di st ar ca pd ps ad de bl { 	
		forv i = 1/7 { 	
		
			replace howcl`x'_w`i' = 10 if alive_w`i' == 1 ///
			& ever`x'_w`i' == .  	///
			&  ((onsetdate`x'1 >= intdate_w`i' & onsetdate`x'1!=. ) ///
			| 	(onsetdate`x'2 >= intdate_w`i' & onsetdate`x'2!=. ) ///
			|   (onsetdate`x'3 >= intdate_w`i' & onsetdate`x'3!=. ) ///
			|   (onsetdate`x'4 >= intdate_w`i' & onsetdate`x'4!=. ) ///
			|   (onsetdate`x'5 >= intdate_w`i' & onsetdate`x'5!=. ) ///
			|   (onsetdate`x'6 >= intdate_w`i' & onsetdate`x'6!=. )  ///
			|   (onsetdate`x'7 >= intdate_w`i' & onsetdate`x'7!=. ))		
		
			replace ever`x'_w`i' = 0 if alive_w`i' == 1 ///
			& ever`x'_w`i' == .  	///
			&  ((onsetdate`x'1 >= intdate_w`i' & onsetdate`x'1!=. ) ///
			| 	(onsetdate`x'2 >= intdate_w`i' & onsetdate`x'2!=. ) ///
			|   (onsetdate`x'3 >= intdate_w`i' & onsetdate`x'3!=. ) ///
			|   (onsetdate`x'4 >= intdate_w`i' & onsetdate`x'4!=. ) ///
			|   (onsetdate`x'5 >= intdate_w`i' & onsetdate`x'5!=. ) ///
			|   (onsetdate`x'6 >= intdate_w`i' & onsetdate`x'6!=. ) ///
			|   (onsetdate`x'7 >= intdate_w`i' & onsetdate`x'7!=. ))

		}		
	}	
	
	
	
	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
	forv i = 1/7 { 
	forv j = 1/7 { 
		if `i' < `j' { 
		di in red "`x' `i' `j'" 
			assert ever`x'_w`i' <= ever`x'_w`j'	| ever`x'_w`i' ==. | ever`x'_w`j'==-1 
	}
	}
	}
	}
 		
	
	preserve 
	local foreshape 
	local fortabout 
	foreach x in an mi hf di st ar ca pd ps ad de bl { 	
			local foreshape `foreshape' onsetdatecleaned`x' 
			local fortabout `fortabout' sum onsetdatecleaned`x' 
	} 
	
	keep idauniq onsetdatecleaned* 
	
	reshape long `foreshape' , i(idauniq) j(wave) 
	tabout wave using how_many_onset_dates_changed_suspected_recall_error_w7.txt, ///
			c(`fortabout') sum replace f(0) 
	restore 
	

	
 save "$healthsave\recoded dates_w7.dta", replace 
